﻿<%@ Page Title="" Language="C#" MasterPageFile="~/YUIAspNet.Master" AutoEventWireup="true" CodeBehind="LazyTreeView.aspx.cs" Inherits="YUIAspNetDemo.TreeView.LazyTreeView" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <title>YUIAsp.Net Controls : LiveDemo : TreeView : Lazy loading</title>
    <script type="text/javascript">
        var loadCallback = false;
    
        function LoadNodes(tree, node) {
            YUIAspNetDemo.TreeView.NodeSource.set_defaultSucceededCallback(function(result, userContext) {
                tree._appendChilds(node, result);
                node.loadComplete();
                
                if(loadCallback && typeof(loadCallback) == "function")
                {
                    loadCallback(tree, node);
                    loadCallback = null;
                }
            });

            YUIAspNetDemo.TreeView.NodeSource.set_defaultFailedCallback(function(result, userContext) {
                node.loadComplete();
                alert("failed to get childs from node: " + userContext.value); 
            });

            YUIAspNetDemo.TreeView.NodeSource.GetChildNodes(node.data, null, null, node.data);
        }
        
        //expand node automatically...
        function Expand(sender, args)
        {
            var tree = $find('<%= this.lazyTree.ClientID %>');
            var nodeToExpand = tree.get_YUIControl().getNodeByProperty("value", "n1");
            
            loadCallback = CustomFunctionAfterLoad;
            
            nodeToExpand.expand();
            
            //LoadNodes(tree, nodeToExpand);
            args.cancel = true;
        }
        
        function CustomFunctionAfterLoad(tree, node)
        {
            alert(node.children[0].label);
        }
        
    </script>
    
    
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<asp:ScriptManagerProxy ID="scriptProxy" runat="server">
    <Services>
        <asp:ServiceReference Path="~/TreeView/NodeSource.asmx" />
    </Services>
</asp:ScriptManagerProxy>

<h4>Lazy TreeView</h4>
<p>In this demo, a javascript callback is defined in order to get the child nodes from the server, by making a call to a ASP.NET web service. The callback takes 2 arguments: the instance of the TreeView and the Node that is expanded. The node is a YUI node instance!</p>
    <p>
        <yui:TreeView ID="lazyTree" OnClientNodePopulate="LoadNodes" LazyLoad="true" runat="server">
            <Nodes>
                <yui:TreeNode Text="node 1" Value="n1"></yui:TreeNode>
                <yui:TreeNode Text="node 2" IsLeaf="true" Value="n2"></yui:TreeNode>
            </Nodes>
        </yui:TreeView>
    </p>
    
    <p><yui:Button ID="btn" runat="server" Text="Expand Node 1" OnClientClick="Expand"></yui:Button></p>
</asp:Content>
